package co.inventario.modelo.fachada;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import co.inventario.entidad.LoginDTO;
import co.inventario.modelo.negocio.LoginNegocio;
import co.inventario.transversal.DAOFactorySQLServer;

public class LoginFachada {
	
	public void crear(LoginDTO loginDTO, Connection connection) throws Exception, SQLException {
		DAOFactorySQLServer daoFactorySQLServer = new DAOFactorySQLServer();
		LoginNegocio loginNegocio = new LoginNegocio();
		try {
			loginNegocio.crear(loginDTO, daoFactorySQLServer.obtenerConexion());
			daoFactorySQLServer.confirmarTransaccion();
		} catch (SQLException exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw new Exception("Se ha presentado un problema intentando crear el login.");
		} catch (Exception exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw exception;
		} finally {
			daoFactorySQLServer.cerrarConexion();
		}
	}

	public void actualizar(LoginDTO loginDTO, Connection connection) throws Exception, SQLException {
		DAOFactorySQLServer daoFactorySQLServer = new DAOFactorySQLServer();
		LoginNegocio loginNegocio = new LoginNegocio();
		try {
			loginNegocio.actualizar(loginDTO, daoFactorySQLServer.obtenerConexion());
			daoFactorySQLServer.confirmarTransaccion();
		} catch (SQLException exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw new Exception("Se ha presentado intentando actualizar el login.");
		} catch (Exception exception) {
			throw exception;
		} finally {
			daoFactorySQLServer.cerrarConexion();
		}
	}

	public ArrayList<LoginDTO> consultarTodo(Connection connection) throws Exception, SQLException {
		DAOFactorySQLServer daoFactorySQLServer = new DAOFactorySQLServer();
		LoginNegocio loginNegocio = new LoginNegocio();
		ArrayList<LoginDTO> loginDTOLista = null;
		try {
			loginDTOLista = loginNegocio.consultarTodo(daoFactorySQLServer.obtenerConexion());
		} catch (SQLException exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw new Exception("Se ha presentado intentando consultar todos los login.");
		} catch (Exception exception) {
			throw exception;
		} finally {
			daoFactorySQLServer.cerrarConexion();
		}
		return loginDTOLista;
	}
}
